package com.gamesnet.ihm;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.gamesnet.api.Api;
import com.gamesnet.models.Console;
import com.gamesnet.widget.ActionBar;
import com.gamesnet.widget.ActionBar.IntentAction;

public class InfoConsoleActivity extends Activity{

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.console);

		Bundle b = getIntent().getExtras();
		long consoleId = b.getLong("consoleId");
		final ActionBar actionBarHaut = (ActionBar) findViewById(R.id.actionbarHaut);
		actionBarHaut.setHomeAction(new IntentAction(this, new Intent(this, DashboardActivity.class), R.drawable.home));

		try {
			Console c = Api.console(String.valueOf(consoleId));
			actionBarHaut.setTitre(c.afficher());

			ImageView imageConsole = (ImageView)findViewById(R.id.pictureConsole);
			URL urlImage = new URL("http://10.0.2.2:8080/Gamesnet/images/upload/resized/consolepicture/"+c.image());
			HttpURLConnection connection = (HttpURLConnection) urlImage.openConnection();
			InputStream inputStream = connection.getInputStream();
			Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
			imageConsole.setImageBitmap(bitmap);
			
			TextView description = (TextView)findViewById(R.id.consoleDescription);
			description.setText(c.getDescription());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}



	}

}
